//
//  HMMainViewController.swift
//  Weibo
//
//  Created by Apple on 2017/7/10.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import SVProgressHUD

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  添加子视图控制器的方式1
//        viewControllers = [xxx,xx]
        //  添加子视图控制器的方式2
//        addChildViewController(xx)
        //  使用kvc给oc只读属性赋值
        let hmTabbar = HMTabBar()
        //  设置代理
        hmTabbar.hmDelegate = self
        //  设置撰写按钮闭包
        hmTabbar.composeButtonCallBack = {[unowned self] in
            print("我是闭包回调过来的")
            print(self)
            
            if !HMUserAccountViewModel.sharedUserAccount.isLogin {
                SVProgressHUD.showError(withStatus: "请先登录，亲~")
                return
            }
            
            let composeView = HMComposeView()
            composeView.show(target: self)
            
            
        }
        self.setValue(hmTabbar, forKey: "tabBar")
        addChildViewController(childController: HMHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(childController: HMMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(childController: HMDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(childController: HMMeTableViewController(), imageName: "tabbar_profile", title: "我的")
    }
    
    //  重载添加子视图控制器的方法
    func addChildViewController(childController: UIViewController, imageName: String, title: String) {
        //  设置tabbaritem的图片
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //  设置字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //  设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 13)], for: .normal)
        
        //  设置标题
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        //  合并一句
        childController.title = title
        //  设置tabbar的tintcolor, 越早设置越好，在appdelegate里面去写
//        UITabBar.appearance().tintColor = UIColor.orange
        //  创建导航控制器
        let nav = HMNavigationController(rootViewController: childController)
        //  添加子视图控制器
        addChildViewController(nav)
        
    }

    

}

//  MARK:   --HMTabBarDelegate
extension HMMainViewController: HMTabBarDelegate {
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
}
